<?php
/*
 * 短信推送
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\facade\Session;
use think\facade\Db;
use yunsms\Yunsms;
use app\common\model\System;

class Sms extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    //查询余额
    public function overage()
    {
        $config = System::get_config_by_code('sms');
        $config['is_sms'] != 1 && $this->error('未开启短信服务，请联系系统管理员！');
        //查询剩余条数
        $res = Yunsms::Overage($config);
        $res['ReturnStatus'] == 'Faild' && $this->error($res['Message']);
        insert_admin_log('查询了短信余额剩'.$res['Overage'].'条');//记录管理员操作
        $this->success('短信剩余 <font color="#FFB800">'.$res['Overage'].'</font> 条');
    }

    //单独发送短信
    public function single()
    {
    	if ($this->request->isPost()) {
            $config = System::get_config_by_code('sms');
            $config['is_sms'] != 1 && $this->error('未开启短信服务，请联系系统管理员！');
            $param = $this->request->param();
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'push');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $phone = explode(",", $param['mobile']);
            $mobile = [];
            foreach ($phone as $key => $v) {if (is_mobile_phone ($v)) {$mobile[] = $v;}}
            !$mobile && $this->error('手机号不能为空');
            $param['mobile'] = implode(',', $mobile);
            //发送短信
            $res = Yunsms::Send($config,$param);
            $res['ReturnStatus'] == 'Faild' && $this->error($res['Message']);
            $res['Message']=='ok' && $res['Message'] = '发送成功!';
            insert_admin_log('发送了短信消息');//记录管理员操作
            $this->success($res['Message']);
        }
        return $this->fetch('smsSend');
    }

    //短信群发送
    public function group()
    {
        if ($this->request->isPost()) {
            $config = System::get_config_by_code('sms');
            $config['is_sms'] != 1 && $this->error('未开启短信服务，请联系系统管理员！');
            $param = $this->request->param();
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'push');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $param['sex']  == -1 && $param['sex'] = '0,1,2';
            $list = Db::table('user')->where('status',1)->where('sex','in',$param['sex'])->column('mobile');
            $mobile = [];
            foreach ($list as $key => $v) {if (is_mobile_phone ($v)) {$mobile[] = $v;}}
            !$mobile && $this->error('手机号不能为空');
            $param['mobile'] = implode(',', $mobile);
            //发送短信
            $res = Yunsms::Send($config,$param);
            $res['ReturnStatus'] == 'Faild' && $this->error($res['Message']);
            $res['Message']=='ok' && $res['Message'] = '发送成功!';
            insert_admin_log('群发了短信消息');//记录管理员操作
            $this->success($res['Message']);

        }
        return $this->fetch('smsMulti');
    }



}